/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Platform.MyGame;
import Platform.User;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 * @author HuuTri
 */
public class Login extends MIDlet implements CommandListener {

    public Image imageBackground;
    
    public TextField txtUserName = new TextField("Username ","",10,TextField.ANY);
    public TextField txtPassword = new TextField("Pasword  ","",10,TextField.PASSWORD);
    public Ticker ticker = new Ticker("Enter username and password to sign in");
    public Command cmdSignIn = new Command("SignIn", Command.OK, 1);
    public Command cmdBack = new Command("Back",Command.OK,1);
    public Command cmdPlay = new Command("Play", Command.OK,1);
    public Command cmdExit = new Command("Exit", Command.OK,2);
    public ChoiceGroup choiceRememberLogin  = new ChoiceGroup("",Choice.MULTIPLE);
    public Spacer spacer = new Spacer(20,80);
    
    public User user = new User("user","user");
    
    public Canvas waiting = new CustomCanvas();
    public Form form = new Form("Sign in here");
    public MyGame game;
    
    public Login() throws IOException, Exception
    {
        imageBackground = Image.createImage("/background.png");
        choiceRememberLogin.append("Remenber me.",null);
        txtUserName.setLayout(Item.LAYOUT_CENTER);
        
        form.append(spacer);
        form.append(txtUserName);
        form.append(txtPassword);
        form.addCommand(cmdSignIn);
        form.setCommandListener(this);
        form.setTicker(ticker);
        form.append(choiceRememberLogin);  
        
        waiting.repaint();
        waiting.addCommand(cmdPlay);
        waiting.addCommand(cmdExit);
        waiting.setCommandListener(this);
        
        game = new MyGame();
        game.addCommand(cmdBack);
        game.setCommandListener(this);
    }
    
    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        System.gc();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSignIn){
            if (txtUserName.getString().equals(user.getName()) &&
                    txtPassword.getString().equals(user.getPassword())) {
                    Display.getDisplay(this).setCurrent(waiting);
            }
            else {
                Alert alert = new Alert("Wrong password or username, try again!");
                alert.setTimeout(500);
                Display.getDisplay(this).setCurrent(alert,form);
            }
        }
        else if (c == cmdPlay)
        {
            Display.getDisplay(this).setCurrent(game);
            game.start();
        }
        else if (c == cmdBack)
        {
            Display.getDisplay(this).setCurrent(waiting);
            game.stop();
        }
        else if (c == cmdExit)
        {
            this.notifyDestroyed();
        }
    }
    
    public void drawScreen(Graphics g){
    }
}

